package core;

import java.util.Iterator;

import util.NameSet;

import core.units.Unit;

/**
 * Level
 * 
 * A level is a container for units. Levels will be iterated over by the Game
 * and the units will be animated. The level has two arrays for removing and
 * adding elements to itself while being iterated over. In order to ensure that
 * the level is up to date the update method must be called.
 * 
 * @version 2.1
 * @author Zachary Cancio
 * @author Rallie Markova
 * @author Alexander Schearer <aas11@duke.edu>
 */
abstract public class Level implements Named<Level>, Iterable<Unit> {

	private String myName;

	private NameSet<Unit> myUnits;

	private NameSet<Unit> to_add;

	private NameSet<Unit> to_remove;

	public Level(String name) {
		myName = name;
		myUnits = new NameSet<Unit>();
		to_add = new NameSet<Unit>();
		to_remove = new NameSet<Unit>();
	}

	public Unit get(String name) {
		return myUnits.get(name);
	}

	public boolean add(Unit s) {
		return to_add.add(s);
	}

	public Iterator<Unit> iterator() {
		return myUnits.iterator();
	}

	public void remove(Unit s) {
		to_remove.add(s);
	}

	public void update() {
		myUnits.addAll(to_add);
		myUnits.removeAll(to_remove);
		to_add.clear();
		to_remove.clear();
	}

	public String getName() {
		return myName;
	}

	public int compareTo(Level l) {
		return myName.compareTo(l.getName());
	}

	public boolean equals(Level l) {
		return myName.equals(l.getName());
	}

	abstract public void reset();
	
	abstract public boolean lost();
}